//
//  PopBaseControl.m
//  HTMAnimations
//
//  Created by Ting Han on 2017/9/14.
//  Copyright © 2017年 Ting Han. All rights reserved.
//

#import "PopBaseControl.h"

@interface PopBaseControl ()<CAAnimationDelegate>

@property (nonatomic, strong) UIView   *absView;
@property (nonatomic, strong) UIButton *button;
@property (nonatomic, strong) UIView   *contentView;

@property (nonatomic) CGFloat percent;

@end

@implementation PopBaseControl

- (void)layoutSubviews {
    
    [super layoutSubviews];
    _button.frame       = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
    _contentView.bounds = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self popBaseControlSetup];
    }
    return self;
}
- (void)popBaseControlSetup{
    // 动画时间
    _animationDuration = 0.4f;
    
    // 隐身的view
    _absView                        = [[UIView alloc] init];
    _absView.userInteractionEnabled = NO;
    _absView.backgroundColor        = [UIColor clearColor];
    [self addSubview:_absView];
    
    // 容器View
    _contentView                        = [[UIView alloc] initWithFrame:self.bounds];
    _contentView.userInteractionEnabled = NO;
    [self addSubview:_contentView];
    
    // 按钮
    _button = [[UIButton alloc] initWithFrame:self.bounds];
    [self addSubview:_button];
    
    // 按钮事件
    [_button addTarget:self action:@selector(touchBeginOrTouchDragEnter) forControlEvents:UIControlEventTouchDown | UIControlEventTouchDragEnter];
    [_button addTarget:self action:@selector(touchUpInside)   forControlEvents:UIControlEventTouchUpInside];
    [_button addTarget:self action:@selector(touchDragExitOrTouchCancel)   forControlEvents:UIControlEventTouchDragExit | UIControlEventTouchCancel];
}


- (void)touchUpInside {
    
    [self touchDragExitOrTouchCancel];
    
    [self controllEventActived];
    
    if (self.target && self.selector) {
        
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
        [self.target performSelector:self.selector withObject:self];
#pragma clang diagnostic pop
    }
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(popBaseControlTouchEvent:)]) {
        
        [self.delegate popBaseControlTouchEvent:self];
    }
}

- (void)touchDragExitOrTouchCancel{
    [_absView.layer removeAllAnimations];
    CABasicAnimation *opacityAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
    opacityAnimation.toValue = @(1);
    opacityAnimation.delegate = self;
    opacityAnimation.duration = _animationDuration;
    [_absView.layer addAnimation:opacityAnimation forKey:@"absViewOpacityAnimation"];
    
}

- (void)touchBeginOrTouchDragEnter{
    [_absView.layer removeAllAnimations];
    CABasicAnimation *opacityAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
    opacityAnimation.toValue = @(0);
    opacityAnimation.delegate = self;
    opacityAnimation.duration = _animationDuration;
    [_absView.layer addAnimation:opacityAnimation forKey:@"absViewOpacityAnimation"];
}


#pragma mark --- CAAnimationDelegate
- (void)animationDidStart:(CAAnimation *)anim{
   //没有实现，需要加一个定时器
//    NSNumber *toValue = (NSNumber *)[anim valueForKeyPath:@"currentValue"];
//    _percent          = (toValue.floatValue - [PopBaseControl calculateConstantWithX1:0 y1:1 x2:1 y2:0]) / [PopBaseControl calculateSlopeWithX1:0 y1:1 x2:1 y2:0];
//    
//    [self currentPercent:_percent];
    
}
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
    
}
#pragma mark --- 被子类继承
- (void)currentPercent:(CGFloat)percent{
    
}
- (void)controllEventActived{
    
}
#pragma mark - Math.

+ (CGFloat)calculateSlopeWithX1:(CGFloat)x1 y1:(CGFloat)y1 x2:(CGFloat)x2 y2:(CGFloat)y2 {
    
    return (y2 - y1) / (x2 - x1);
}

+ (CGFloat)calculateConstantWithX1:(CGFloat)x1 y1:(CGFloat)y1 x2:(CGFloat)x2 y2:(CGFloat)y2 {
    
    return (y1*(x2 - x1) - x1*(y2 - y1)) / (x2 - x1);
}

@end
